﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FrmMarkRiserpipe : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public MarkPipeType MarkingType { get; set; }

		public FrmMarkRiserpipe()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{0410EE7D-7A91-45FA-8388-CD1BBD7A08FD}", null);
		}

		private void FrmMarkRiserpipe_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.Icon = Resource.YArchitechIcon;
			this.radioSinglePipe.Checked = (MarkRiserPipeData.Instance.TypeOfMarking == MarkPipeType.singlePipeMarking);
			this.radioMultiPipe.Checked = (MarkRiserPipeData.Instance.TypeOfMarking == MarkPipeType.multiPipeMarking);
			this.radioAutoIncrease.Checked = (MarkRiserPipeData.Instance.NumberCreating == CreateNumber.increaseWithSystem);
			this.radioCustom.Checked = (MarkRiserPipeData.Instance.NumberCreating == CreateNumber.custom);
			this.textCustom.Enabled = this.radioCustom.Checked;
			this.checkAreaNumber.Checked = MarkRiserPipeData.Instance.IsUseAreaNumber;
			this.textAreaNumber.Enabled = this.checkAreaNumber.Checked;
			this.checkFloorNumber.Checked = MarkRiserPipeData.Instance.IsUseFloorNumber;
			this.textFloorNumber.Enabled = this.checkFloorNumber.Checked;
			this.checkLineAngle.Checked = MarkRiserPipeData.Instance.IsUseAngle;
			this.textLineAngle.Enabled = this.checkLineAngle.Checked;
			this.textCustom.Text = MarkRiserPipeData.Instance.customNumber;
			this.textAreaNumber.Text = MarkRiserPipeData.Instance.AreaNumber;
			this.textFloorNumber.Text = MarkRiserPipeData.Instance.FloorNumber;
			this.textLineAngle.Text = MarkRiserPipeData.Instance.Angle;
			this.textDisToPipe.Text = MarkRiserPipeData.Instance.Distance;
			this.MarkingType = MarkRiserPipeData.Instance.TypeOfMarking;
			this.checkLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.textLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.textDisToPipe.Enabled = this.radioSinglePipe.Checked;
			this.textLineAngle.Enabled = this.checkLineAngle.Checked;
		}

		private void radioSinglePipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioSinglePipe.Checked)
			{
				this.MarkingType = MarkPipeType.singlePipeMarking;
				this.isPicking = true;
				this.SwithStatus();
				this.isPicking = true;
			}
		}

		private void radioMultiPipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioMultiPipe.Checked)
			{
				this.MarkingType = MarkPipeType.multiPipeMarking;
				this.isPicking = true;
				this.SwithStatus();
				this.isPicking = true;
			}
			this.checkLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.textLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.textDisToPipe.Enabled = this.radioSinglePipe.Checked;
		}

		private void radioIncrease_CheckedChanged(object sender, EventArgs e)
		{
			this.textCustom.Enabled = this.radioCustom.Checked;
		}

		private void radioCustom_CheckedChanged(object sender, EventArgs e)
		{
			this.textCustom.Enabled = this.radioCustom.Checked;
		}

		private void checkAreaNumber_CheckedChanged(object sender, EventArgs e)
		{
			this.textAreaNumber.Enabled = this.checkAreaNumber.Checked;
		}

		private void checkFloorNumber_CheckedChanged(object sender, EventArgs e)
		{
			this.textFloorNumber.Enabled = this.checkFloorNumber.Checked;
		}

		private void checkLineAngle_CheckedChanged(object sender, EventArgs e)
		{
			this.textLineAngle.Enabled = this.checkLineAngle.Checked;
		}

		protected void SwithStatus()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X + 20, Cursor.Position.Y);
			this.SetFocusToMainWnd();
			this.PressESCKey();
		}

		protected void SetFocusToMainWnd()
		{
		}

		protected void PressESCKey()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FrmMarkRiserpipe.keybd_event((byte)bVk, 0, dwFlags, 0);
			FrmMarkRiserpipe.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void textCustom_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.textCustom.Text))
			{
				return;
			}
			try
			{
				Convert.ToDouble(this.textCustom.Text);
			}
			catch (Exception)
			{
				YJKMessageBox.Error("编号必须是数字，请重新输入!");
				this.textCustom.Text = MarkRiserPipeData.Instance.Angle;
			}
		}

		private void textLineAngle_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.textLineAngle.Text))
			{
				return;
			}
			try
			{
				double num = Convert.ToDouble(this.textLineAngle.Text);
				if (num < 0.0 || num > 90.0)
				{
					YJKMessageBox.Error("请输入0-90之间的角度值!");
					this.textLineAngle.Text = MarkRiserPipeData.Instance.Angle;
				}
			}
			catch (Exception)
			{
			}
		}

		private void textDisToPipe_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.textDisToPipe.Text))
			{
				return;
			}
			try
			{
				if (Convert.ToDouble(this.textDisToPipe.Text) < 0.0)
				{
					YJKMessageBox.Error("输入值必须大于0，请重新输入!");
					this.textDisToPipe.Text = MarkRiserPipeData.Instance.Distance;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Error("输入值必须是数字，请重新输入!");
				this.textDisToPipe.Text = MarkRiserPipeData.Instance.Distance;
			}
		}

		private void FrmMarkRiserpipe_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (this.radioSinglePipe.Checked)
			{
				MarkRiserPipeData.Instance.TypeOfMarking = MarkPipeType.singlePipeMarking;
			}
			if (this.radioMultiPipe.Checked)
			{
				MarkRiserPipeData.Instance.TypeOfMarking = MarkPipeType.multiPipeMarking;
			}
			if (this.radioAutoIncrease.Checked)
			{
				MarkRiserPipeData.Instance.NumberCreating = CreateNumber.increaseWithSystem;
			}
			if (this.radioCustom.Checked)
			{
				MarkRiserPipeData.Instance.NumberCreating = CreateNumber.custom;
			}
			MarkRiserPipeData.Instance.IsUseAreaNumber = this.checkAreaNumber.Checked;
			MarkRiserPipeData.Instance.IsUseFloorNumber = this.checkFloorNumber.Checked;
			MarkRiserPipeData.Instance.IsUseAngle = this.checkLineAngle.Checked;
			MarkRiserPipeData.Instance.customNumber = this.textCustom.Text;
			MarkRiserPipeData.Instance.AreaNumber = this.textAreaNumber.Text;
			MarkRiserPipeData.Instance.FloorNumber = this.textFloorNumber.Text;
			MarkRiserPipeData.Instance.Angle = this.textLineAngle.Text;
			MarkRiserPipeData.Instance.Distance = this.textDisToPipe.Text;
			this.PressESCKey();
			this.isPicking = false;
		}

		private void FrmMarkRiserpipe_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		public bool isPicking = true;

		private ControlInfoManager m_thisManger;
	}
}
